<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2007 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version

  $Source$
  $Revision$
  $Author$
  $Date$
**********************************************/
if (!defined('ADMIN_PAGES')) { exit; }
require_once('header.php');
require_once(CORE_PATH.'nbbcode.php');
/*
$cpgtpl->set_filenames(array('body' => 'admin/index_body.html'));
$cpgtpl->display('body');
$cpgtpl->destroy();
*/

if (isset($_POST['np_save'])) {
	$np_notes = Fix_Quotes(check_words($_POST['np_notes']));
	Dragonfly::getKernel()->CFG->set('notepad', 'text', $np_notes);
	URL::redirect(URL::admin());
}
if ((isset($_POST['np_lock']) || isset($_POST['np_unlock'])) && can_admin()) {
	Dragonfly::getKernel()->CFG->set('notepad', 'lock', intval(isset($_POST['np_lock'])));
	URL::redirect(URL::admin());
}

$TPL = Dragonfly::getKernel()->OUT;
$TPL->CPG = $CPG_SESS;

if (can_admin() && $MAIN_CFG['global']['update_monitor']) {
	if (!isset($CPG_SESS['update_monitor'])) { $CPG_SESS['update_monitor'] = false; }
	if ($CPG_SESS['update_monitor']) {
		$CPG_SESS['update_monitor'] = Cache::array_load('data', 'update_monitor', true);
	}
	if (!$CPG_SESS['update_monitor']) {
		$update_url = 'http://dragonflycms.org/update.php?vers='.CPG_NUKE;
		$updinfo = get_fileinfo($update_url, false, true);
		if ($updinfo) {
			$items = preg_split('#(<item>)#s', $updinfo['data'], -1, PREG_SPLIT_NO_EMPTY);
			unset($updinfo);
			$curvers = preg_replace('#^(.*)<version>(.*)</version>(.*)#s','\\2',$items[0], 1);
			$upgurl = preg_replace('#^(.*)<url>(.*)</url>(.*)#s','\\2',$items[0], 1);
			unset($items[0]);
			$data = array('current'=>$curvers, 'url'=>$upgurl, 'num'=>count($items), 'msg'=>array());
			foreach ($items as $item) {
				if (!empty($item)) {
					$alrt_vers = preg_replace('#(.*)<version>(.*)</version>(.*)#s','\\2',$item);
					$alrt_title = preg_replace('#(.*)<title>(.*)</title>(.*)#s','\\2',$item);
					$alrt_desc = preg_replace('#(.*)<description>(.*)</description>(.*)#s','\\2',$item);
					$alrt_date = preg_replace('#(.*)<date>(.*)</date>(.*)#s','\\2',$item);
					$data['msg'][] = array('vers'=>$alrt_vers, 'title'=>$alrt_title, 'desc'=>$alrt_desc, 'date'=>$alrt_date);
				}
			}
			$data['using_latest'] = version_compare(CPG_NUKE, $data['current'], '>=');
			$data['using_cvs']    = version_compare(CPG_NUKE, $data['current'], '>');
			$TPL->CPG['update_monitor'] = $CPG_SESS['update_monitor'] = $data;
			Cache::array_save('data', 'update_monitor', $data);
		}
	}
}

// GraphicAdmin() can't be directly used in Poodle_TPL so we push the buffer
ob_start();
GraphicAdmin();
$TPL->admin_menu = ob_get_clean();

/*
 * Stats
 */

$result = $db->query('SELECT COUNT(*), guest FROM '.$prefix.'_session GROUP BY guest ORDER BY guest');
$online_num = array(0, 0, 0, 0);
while ($row = $result->fetch_row()) {
	$online_num[$row[1]] = intval($row[0]);
}
$result->free();
$TPL->online_num = $online_num;

$day = L10NTime::tolocal((mktime(0,0,0,date('n'),date('j'),date('Y'))-date('Z')), $userinfo['user_dst'], $userinfo['user_timezone']);
$TPL->new_members = array(
	'today' => $db->count('users',"user_regdate>={$day}"),
	'yesterday' => $db->count('users',"user_regdate<{$day} AND user_regdate>=".($day-86400)),
);

if (is_active('Surveys') && can_admin('surveys')) {
	$TPL->current_poll = $db->uFetchAssoc("SELECT poll_id id, poll_title title FROM {$db->TBL->poll_desc} WHERE artid=0 ORDER BY poll_id DESC");
}

$TPL->display('admin/index');
//echo $TPL->toString('admin/index');
